#ifndef _U_LINKEDLISTS_H
#define _U_LINKEDLISTS_H

#include <io2.h>
#include <mm.h>

typedef struct __node
{
	int value;
	struct Node *next;
} Node;


static Node *startNode = NULL;


/* Node *mynode = NULL;
 * mynode = makeNode (123);
 * addNode (mynode); */


int makeNode (int value)
{
	Node *newNode;

	newNode = (Node *)malloc(sizeof(Node));
	
	if(newNode != NULL) {
		newNode->value = value;
		newNode->next  = NULL;
	}

	return newNode;
}


void addNode (Node *newNode)
{
	Node *startNodeCopy = startNode;

	if (startNode == NULL) {
		startNode = newNode;
	}
	else {
		while (startNodeCopy->next != NULL) {
			startNodeCopy = startNodeCopy->next;
		}
		
		startNodeCopy->next = newNode;
	}    
}


void showList (void)
{
	unsigned count = 0;
	Node *startNodeCopy = startNode;

	if (startNode == NULL) {
		printf ("There's no nodes in the list!\n");
		return;
	}

	while (startNodeCopy != NULL) {
	        printf ("Node #%d\n", count);
	        printf (" + Value: %d\n\n", startNodeCopy->value);
	        
	        count ++;
	        startNodeCopy = startNodeCopy->next;
	}
}

void searchNode (int value)
{
	unsigned current = 0, found = 0;
	Node *startNodeCopy = startNode;

	if (startNode == NULL) {
		printf ("There's no nodes in the list\n");
		return;
	}

	while (startNodeCopy != NULL) {
		if (startNodeCopy->value == value) {
			printf ("Node #%d has the value, you searched for\n", current);
			found ++;
		}

		current ++;
		startNodeCopy = startNodeCopy->next;
	}

	if (found == 0) {
		printf ("No node contains the value, you searched for.\n");
	}
}

#endif
